package com.chinawlx.wlxteacher.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.network.bean.WLXClazzDynamicBean;
import com.chinawlx.wlxteacher.ui.activity.WLXPhotoViewActivity;
import com.chinawlx.wlxteacher.ui.activity.WLXTeachIdeaActivity;
import com.chinawlx.wlxteacher.ui.activity.WLXUnReadNumberActivity;
import com.chinawlx.wlxteacher.utils.WLXDateUtil;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.utils.WLXLogUtil;
import com.chinawlx.wlxteacher.utils.WLXTeacherConstant;
import com.chinawlx.wlxteacher.wlx_grade_notice;
import com.chinawlx.wlxteacher.wlx_schedule;
import com.chinawlx.wlxteacher.wlx_scheduleDao;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * 班级动态适配器
 * <p>
 * Created by wlx on 16/10/11.
 */

public class WLXDynamicAdapter extends BaseAdapter {
    private Context mContext;
    private List<wlx_grade_notice> gradeNotices;

    public WLXDynamicAdapter(Context mContext) {
        this.mContext = mContext;
    }
    public void setData(List<wlx_grade_notice> gradeNotices){
//        this.gradeNotices.clear();
//        this.gradeNotices.addAll(gradeNotices);
        this.gradeNotices=gradeNotices;

    }
    public void addData(List<wlx_grade_notice> gradeNotices){
        this.gradeNotices.addAll(gradeNotices);

    }
    @Override
    public int getCount() {
        return gradeNotices == null ? 0 : gradeNotices.size();
    }

    @Override
    public Object getItem(int position) {
        return gradeNotices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DynamicViewHolder holder = null;
        Gson gson = new Gson();
        final WLXClazzDynamicBean.DataBean.GradeNoticeListBean.ExtendBean extendBean = gson
                .fromJson
                        (gradeNotices.get(position).getExtend_info(), new
                                TypeToken<WLXClazzDynamicBean.DataBean.GradeNoticeListBean
                                        .ExtendBean>() {
                                }.getType());
        if (convertView == null) {
            holder = new DynamicViewHolder();
            convertView = View.inflate(mContext, R.layout.item_clazz_dynamic, null);
            holder.imageLayout = (LinearLayout) convertView.findViewById(R.id
                    .ll_image_clazz_dynamic);
            holder.iamge1 = (ImageView) convertView.findViewById(R.id.iv_image1_clazz_dynamic);
            holder.iamge2 = (ImageView) convertView.findViewById(R.id.iv_image2_clazz_dynamic);
            holder.iamge3 = (ImageView) convertView.findViewById(R.id.iv_image3_clazz_dynamic);
            holder.imageNum = (TextView) convertView.findViewById(R.id
                    .tv_image_num_clazz_dynamic);
            holder.content = (TextView) convertView.findViewById(R.id.tv_content_clazz_dynamic);
            holder.line = (ImageView) convertView.findViewById(R.id.iv_line_clazz_dynamic);
            holder.date = (TextView) convertView.findViewById(R.id.tv_date_clazz_dynamic);
            holder.idea = (TextView) convertView.findViewById(R.id.tv_idea_clazz_dynamic);
            holder.ideaNum = (TextView) convertView.findViewById(R.id.tv_idea_num_clazz_dynamic);
            holder.read = (TextView) convertView.findViewById(R.id.tv_read_clazz_dynamic);
            convertView.setTag(holder);
        } else {
            holder = (DynamicViewHolder) convertView.getTag();
        }
        holder.content.setText(gradeNotices.get(position).getContent());
        String date = WLXDateUtil.getTimeStampToDate(gradeNotices.get(position)
                .getCreation_date());
        holder.date.setText(date.substring(0, date.lastIndexOf(" ")));

        final List<String> resource_url = extendBean.getResource_url();
        List<ImageView> imageList = new ArrayList<>();
        imageList.add(holder.iamge1);
        imageList.add(holder.iamge2);
        imageList.add(holder.iamge3);
        if (resource_url.size() > 0) {
            holder.imageLayout.setVisibility(View.VISIBLE);
            holder.line.setVisibility(View.VISIBLE);
            for (int i = 0; i < imageList.size(); i++) {
                Glide.with(mContext).load(resource_url.get(i)).into(imageList.get(i));
                if (resource_url.size() - i - 1 <= 0) {
                    break;
                }
            }
            if (resource_url.size() - imageList.size() <= 0) {
                holder.imageNum.setVisibility(View.GONE);
            } else {
                holder.imageNum.setVisibility(View.VISIBLE);
                holder.imageNum.setText("+" + Integer.toString(resource_url.size() - 3));
            }
            holder.imageLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ArrayList<String> urls = new ArrayList<>();
                    urls.addAll(resource_url);
                    Intent intent = new Intent(mContext,
                            WLXPhotoViewActivity.class);
                    intent.putExtra(WLXPhotoViewActivity.URLS, urls);
                    mContext.startActivity(intent);
                }
            });
        } else {
            holder.imageLayout.setVisibility(View.GONE);
            holder.imageNum.setVisibility(View.GONE);
            holder.line.setVisibility(View.GONE);
        }
        //教学意见,为阅人数初始化为GONE
        holder.read.setVisibility(View.GONE);
        holder.idea.setVisibility(View.GONE);
        holder.ideaNum.setVisibility(View.GONE);
        if (gradeNotices.get(position).getType_code().equals("schedule")) {
            //-------family_user_ids不为空时,未阅人数可见-------
            String family_user_ids = gradeNotices.get(position).getFamily_user_ids();
            if (!TextUtils.isEmpty(family_user_ids)) {
                final String[] ids = family_user_ids.split(",");
                holder.read.setVisibility(View.VISIBLE);
                holder.read.setText(ids.length + "人未阅");
                //将教学意见不可见
                holder.idea.setVisibility(View.GONE);
                holder.ideaNum.setVisibility(View.GONE);
//            WLXLogUtil.printE("--- true: ---" + gradeNotices.get(position).getFamily_user_ids());
//            WLXLogUtil.printE("--- ids: ---" + ids[0]);
                holder.read.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, WLXUnReadNumberActivity.class);
                        intent.putExtra(WLXTeacherConstant.FAMILY_USER_IDS, ids);
                        mContext.startActivity(intent);
                    }
                });
            }
            //-------getScore_bad大于0时,教学意见可见-------
            final String resourceId = gradeNotices.get(position).getResource_id().toString();
            List<wlx_schedule> wlxSchedules = WLXGreenDaoUtil.getScheduleDao().queryBuilder().where
                    (wlx_scheduleDao.Properties.Schedule_id.eq(resourceId)).list();
            WLXLogUtil.printE("--- resourceId: " + resourceId + "--- schedules: " + wlxSchedules.size());
            //教学意见可见
            if (wlxSchedules.size()>0&&wlxSchedules.get(0).getScore_bad()>0) {
                WLXLogUtil.printE("--- scheduleID: " + wlxSchedules.get(0).getSchedule_id() +
                        "--- getScore_bad: " + wlxSchedules.get(0).getScore_bad());
                Integer score_bad = wlxSchedules.get(0).getScore_bad();
                //未阅人数不可见
                holder.read.setVisibility(View.GONE);
                //教学意见可见
                holder.idea.setVisibility(View.VISIBLE);
                holder.ideaNum.setVisibility(View.VISIBLE);
                holder.ideaNum.setText(score_bad + "");
                holder.idea.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, WLXTeachIdeaActivity.class);
                        intent.putExtra(WLXTeacherConstant.BAD_SCHEDULE_ID,resourceId);
                        mContext.startActivity(intent);
                    }
                });

            }
        }

        return convertView;
    }

    static class DynamicViewHolder {
        LinearLayout imageLayout;
        ImageView iamge1;
        ImageView iamge2;
        ImageView iamge3;
        TextView imageNum;
        TextView content;
        ImageView line;
        TextView date;
        TextView idea;      //教学意见
        TextView ideaNum;
        TextView read;      //未阅人数

    }
}
